package com.xjscrm.console.service.customer.wwCustomer.impl;

import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.trigger.MicaTriggerBatchMarkLabelMsg;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.WwCustomer;
import com.xjscrm.common.entity.WwCustomerTags;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.entity.WwTags;
import com.xjscrm.common.enums.DeviceTypeEnum;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.mapper.WwCustomerMapper;
import com.xjscrm.console.mapper.WwCustomerTagsMapper;
import com.xjscrm.console.mapper.WwDeviceMapper;
import com.xjscrm.console.mapper.WwTagsMapper;
import com.xjscrm.console.mq.producer.SendMsgProducer;
import com.xjscrm.console.param.wwCustomer.BatchDeleCustomerDto;
import com.xjscrm.console.param.wwCustomer.WwCustomerTagsQuery;
import com.xjscrm.console.service.customer.wwCustomer.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private WwCustomerMapper wwCustomerMapper;

    @Autowired
    private WwCustomerTagsMapper wwCustomerTagsMapper;

    @Autowired
    private WwTagsMapper wwTagsMapper;

    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Autowired
    private WwDeviceMapper wwDeviceMapper;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleCustomer(BatchDeleCustomerDto batchDeleCustomerDto) {
        //获取客户的wxId
        WwCustomer wwCustomer = wwCustomerMapper.getById(batchDeleCustomerDto.getCustomerId(),  batchDeleCustomerDto.getMerId());
        //先删除客户
        int i = wwCustomerMapper.deleById(batchDeleCustomerDto.getCustomerId(), batchDeleCustomerDto.getMerId());
        if(i!=1){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
        }
        //再删除客户对应的标签关系
        WwCustomerTagsQuery wwCustomerTagsQuery=new WwCustomerTagsQuery();
        wwCustomerTagsQuery.setMerId(batchDeleCustomerDto.getMerId());
        wwCustomerTagsQuery.setCustomerId(batchDeleCustomerDto.getCustomerId());
        List<WwCustomerTags> wwCustomerTagsList = wwCustomerTagsMapper.getWwCustomerTags(wwCustomerTagsQuery);
        if(wwCustomerTagsList!=null&&!wwCustomerTagsList.isEmpty()){
            for (WwCustomerTags wwCustomerTags:wwCustomerTagsList) {
                wwCustomerTagsMapper.deleCustomerTags(wwCustomerTags.getId(),wwCustomerTags.getMerId());

                //获取推送过来的标签ID
                WwTags wwtags = wwTagsMapper.getById(wwCustomerTags.getTagId(), wwCustomerTags.getMerId());

                WwDevice wwDevice = wwDeviceMapper.getById(wwCustomer.getDeviceUniqueId(), batchDeleCustomerDto.getMerId());

                MicaTriggerBatchMarkLabelMsg micaTriggerChangeLabelMsg=new MicaTriggerBatchMarkLabelMsg();
                micaTriggerChangeLabelMsg.setDelWxIds(Arrays.asList(wwCustomer.getWxid()));
                micaTriggerChangeLabelMsg.setLabelId(wwtags.getTagId());
                micaTriggerChangeLabelMsg.setIsCorp(wwtags.getType() ==1);
                MicaTriggerDto micaTriggerDto= MicaTriggerDto.newInstance(micaTriggerChangeLabelMsg, MicaTriggerType.TRIGGER_BATCH_MARK_LABEL);
                micaTriggerDto.setMerId(batchDeleCustomerDto.getMerId());
                micaTriggerDto.setDeviceId(wwDevice.getDeviceId());
                micaTriggerDto.setDeviceType(DeviceTypeEnum.DT_ANDROID_WORK_WECHAT_VALUE);
                sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);
            }
        }

    }
}
